from pygame import *
from config_reader import *
from tool import *


class Setting:
    def __init__(self):
        init()
        self.screen = display.set_mode((650, 500))
        self.word = reader().word.get("setting")
        self.aks_font = font.SysFont("MicrosoftYaHei", 25, False)
        display.set_caption("默写器")
        self.screen.fill("lightskyblue")
        self.button_font = font.SysFont("Microsoft YaHei", 25, True, "black")
        self.text_font = font.SysFont("MicrosoftYaHei", 25, True, "black")
        self.title_font = font.SysFont("MicrosoftYaHei", 30, True, "black")
        self.exe_language = reader().exe_language
        self.all_language = reader().all_language
        self.language_pos = self.all_language.index(self.exe_language)

    def main(self):
        self.setting = self.title_font.render(self.word.get("setting"),False,"black")
        self.set_language = self.text_font.render(self.word.get("set_language"),False,"black")
        #self.exe_language = self.word.get(self.language)
        self.chang_language = self.text_font.render(self.exe_language, False, "black","pink")
        self.back = self.text_font.render(self.word.get("back"), False, "black", "pink")

        def pain():
            self.exe_language = self.all_language[self.language_pos]
            #print(self.word.get("setting"))
            self.setting = self.title_font.render(self.word.get("setting"), False, "black")
            self.set_language = self.text_font.render(self.word.get("set_language"), False, "black")
            # self.exe_language = self.word.get(self.language)
            self.chang_language = self.text_font.render(self.exe_language, False, "black", "pink")
            self.back = self.text_font.render(self.word.get("back"), False, "black", "pink")
            self.screen.fill("lightskyblue")

            self.screen.blit(self.setting,(230,10))
            self.screen.blit(self.set_language,(20,70))
            self.screen.blit(self.back, (20, 450))
            self.screen.blit(self.chang_language, (20 + self.set_language.get_width() + 5, 70))


        while True:
            for eve in event.get():
                if (eve.type == MOUSEBUTTONDOWN):
                    print(eve.pos)
                    if(Tool().button(self.chang_language,(20 + self.set_language.get_width() + 5,70),eve.pos)):
                        if(self.language_pos < len(self.all_language)-1):
                            self.language_pos += 1
                            self.exe_language = self.all_language[self.language_pos]
                        else:
                            self.language_pos = 0
                            self.exe_language = self.all_language[self.language_pos]
                        #self.word = reader().re_word().get("setting")
                        print(self.word)
                        print(self.language_pos)
                        Tool().change_cfg("./config.cfg","base","exe_language",self.exe_language)
                        self.exe_language = self.all_language[self.language_pos]
                        self.word = reader().re_word().get("setting")

                    if(Tool().button(self.back, (20, 450),eve.pos)):
                        return "face"

            pain()
            display.update()